package top.serms.leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: SerMs
 * @Date: 2024/07/06 10 51
 * @Email: 1839928782@qq.com
 * @Blog: https://serms.top
 * <p>
 * 需求分析:
 * 1. nums是一个整数数组,包含负数和正数
 * 2. 返回的res数量需要满足:
 * a: 0 <= i < j < n (必须是正数,返回的值不能是非负数)
 * b: nums[i] + nums[j] < target  nums下标i和j的值要 < target
 **/
public class LeetCode2824 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
//        [-1,1,2,3,1]
        list.add(-1);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(1);
        int i = countNumberBinarySearch(list, 2);
        System.out.println(i);
    }

    /**
     * 双指针 : 快慢指针,要求元素必须有序
     *
     * @param nums
     * @param target
     * @return
     */
    public static int SerMsTest02(List<Integer> nums, int target) {
        // 先让元素有序
        Collections.sort(nums);
        int res = 0;
        for (int i = 0, j = nums.size() - 1; i < j; i++) {
            while (i < j && nums.get(i) + nums.get(j) >= target) {
                j--;
            }
            res += j - i;
        }
        return  res;
    }


    /**
     * 暴力枚举
     *
     * @param nums
     * @param target
     * @return
     */
    public static int SerMsTest01(List<Integer> nums, int target) {
        // 枚举i ,假设i跟所有j元素去适配
        int res = 0;
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i + 1; j < nums.size(); j++) {
                int sum = nums.get(i) + nums.get(j);
                if (i < j && sum < target) {
                    res++;
                }
            }
        }
        return res;
    }


    /**
     * 双指针解法
     *
     * @param nums
     * @param target
     * @return
     */
    public static int countNumberBinarySearch(List<Integer> nums, int target) {
        int res = 0;
        Collections.sort(nums);

        for (int i = 0, j = nums.size() - 1; i < j; i++) {
            while (i < j && nums.get(i) + nums.get(j) >= target) {
                j--;
            }
            res += j - i;
        }
        return res;
    }


    /**
     * 双指针解法
     *
     * @param nums
     * @param target
     * @return
     */
    public static int countPairsBinarySearch(List<Integer> nums, int target) {
        // 先保证数组有序
        Collections.sort(nums);
        int res = 0;
        for (int i = 1; i < nums.size(); i++) {
            res += binarySearch(nums, 0, i - 1, target - nums.get(i));
        }
        return res;
    }

    public static int binarySearch(List<Integer> nums, int lo, int hi, int target) {
        int res = hi + 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            if (nums.get(mid) >= target) {
                res = mid;
                hi = mid - 1;
            } else {
                lo = mid + 1;
            }
        }
        return res;
    }


    /**
     * 枚举思想
     * 时间复杂度为O(n2)
     * 空间复杂度为O(1)
     *
     * @param nums
     * @param target
     * @return
     */
    public static int countPairs(List<Integer> nums, int target) {
        int res = 0;
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i + 1; j < nums.size(); j++) {
                if (nums.get(i) + nums.get(j) < target) {
                    res++;
                }
            }
        }
        return res;
    }
}
